package ro.pub.cs.stadium.platform;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;

public class SettingsWindow implements SelectionListener {
	private Shell wndShell = null;
	
	private Button okButton = null;
	private Button cancelButton = null;
	
	private ScrolledComposite propContainer = null;
	private List propList = null;
	
	private Sash splitter = null;
	private FormData listData = null;
	
	private int modalResult = -1;
	
	public SettingsWindow(Shell mainWnd) {
		wndShell = new Shell(mainWnd, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
		
		okButton = new Button(wndShell, SWT.PUSH);
		okButton.setText("&OK");
		
		cancelButton = new Button(wndShell, SWT.PUSH);
		cancelButton.setText("&Cancel");
		
		FormLayout layout = new FormLayout();
		layout.marginWidth = layout.marginHeight = 10;
		wndShell.setLayout(layout);
		
		FormData okData = new FormData();
		okData.bottom = new FormAttachment(100);
		okData.width = 100;
		okData.right = new FormAttachment(cancelButton, -10);
		okButton.setLayoutData(okData);
		
		FormData cancelData = new FormData();
		cancelData.right = new FormAttachment(100);
		cancelData.bottom = new FormAttachment(100);
		cancelData.width = 100;
		cancelButton.setLayoutData(cancelData);	
		
		// Button separator
		Label buttonSep = new Label(wndShell, SWT.SEPARATOR | SWT.SHADOW_OUT
				| SWT.HORIZONTAL);
		
		FormData sepData = new FormData();
		sepData.left = new FormAttachment(0);
		sepData.right = new FormAttachment(100);
		sepData.bottom = new FormAttachment(okButton, -10);
		sepData.height = 2;
		buttonSep.setLayoutData(sepData);
		
		// Options list
		
		propList = new List(wndShell, SWT.BORDER | SWT.V_SCROLL);
		
		
		listData = new FormData();
		listData.left = new FormAttachment(0);
		listData.top = new FormAttachment(0);
		listData.bottom = new FormAttachment(buttonSep, -10);
		listData.width = 200;
		propList.setLayoutData(listData);
		
		// Splitter
		
		splitter = new Sash(wndShell, SWT.VERTICAL);
		FormData splitData = new FormData();
		splitData.left = new FormAttachment(propList);
		splitData.top = new FormAttachment(0);
		splitData.bottom = new FormAttachment(buttonSep, -10);
		splitter.setLayoutData(splitData);
		
		
		// Properties container
		propContainer = new ScrolledComposite(wndShell, SWT.NONE);
		FormData contData = new FormData();
		contData.left = new FormAttachment(splitter);
		contData.right = new FormAttachment(100);
		contData.top = new FormAttachment(0);
		contData.bottom = new FormAttachment(buttonSep, -10);
		propContainer.setLayoutData(contData);	
		
		
		wndShell.setDefaultButton(okButton);
		
		
		okButton.addSelectionListener(this);
		cancelButton.addSelectionListener(this);
		splitter.addSelectionListener(this);
	}
	
	public int showModal() {
		modalResult = -1;
		wndShell.open();
		return modalResult;
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void widgetSelected(SelectionEvent e) {
		if (e.widget == okButton) {
			modalResult = 0;
			wndShell.close();
		}
		
		if (e.widget == cancelButton) {
			modalResult = -1;
			wndShell.close();
		}
		
		if (e.widget == splitter) {
			Rectangle splitRect = splitter.getBounds();
			Rectangle listRect = propList.getBounds();
			
			if (splitRect.x != e.x) {
				listData.width = listRect.width + (e.x - splitRect.x) - 20; 
				wndShell.layout();
			}
		}
		
	}
}
